package com.aquarium.ui;

import com.aquarium.R;
import com.aquarium.alias.AliasManager;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Toast;

public class AliasSettingActivity extends Activity implements
		OnItemSelectedListener {

	private static final String TAG = "AliasSettingActivity";
	private Context mContext;

	private ImageView mOkButton;
	private ImageView mCancelButton;
	private Spinner[] mSpinner = new Spinner[6];
	private int[] mSelectedId;

	private ArrayAdapter<CharSequence> mArrayAdapter;
	private AliasManager mAliasManager;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.controller_setting);

		mContext = AliasSettingActivity.this;
		mAliasManager = AliasManager.getInstance(mContext);
		// mBtManager = BTManager.getInstance();

		mOkButton = (ImageView) findViewById(R.id.ok);
		mCancelButton = (ImageView) findViewById(R.id.cancel);

		mSpinner[0] = (Spinner) findViewById(R.id.controllers_spinner1);
		mSpinner[1] = (Spinner) findViewById(R.id.controllers_spinner2);
		mSpinner[2] = (Spinner) findViewById(R.id.controllers_spinner3);
		mSpinner[3] = (Spinner) findViewById(R.id.controllers_spinner4);
		mSpinner[4] = (Spinner) findViewById(R.id.controllers_spinner5);
		mSpinner[5] = (Spinner) findViewById(R.id.controllers_spinner6);

		mArrayAdapter = ArrayAdapter
				.createFromResource(mContext, R.array.controllers_array,
						android.R.layout.simple_spinner_item);

		mSelectedId = mAliasManager.getControllerType();

		for (int i = 0; i < 6; i++) {
			mSpinner[i].setAdapter(mArrayAdapter);
			mSpinner[i].setSelection(mSelectedId[i]);
			mSpinner[i].setOnItemSelectedListener(this);
		}

		mOkButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				for (int i = 0; i < 6; i++) {
					mSelectedId[i] = mSpinner[i].getSelectedItemPosition();

					// one controller only appear once.
					if (mSelectedId[i] != 0) {
						for (int j = 0; j < i; j++) {
							if (mSelectedId[i] == mSelectedId[j]) {
								Toast.makeText(
										mContext,
										getText(R.string.controller_duplication),
										Toast.LENGTH_SHORT).show();
								return;
							}
						}
					}

				}
				mAliasManager.setSelectedId(mSelectedId);
				finish();

			}
		});

		mCancelButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}

	@Override
	public boolean onKeyUp(int keyCode, KeyEvent event) {

		if (keyCode == KeyEvent.KEYCODE_BACK) {
			for (int i = 0; i < 6; i++) {
				if (mSelectedId[i] != mSpinner[i].getSelectedItemPosition()) {
					requestSaveSettings();
					return true;
				}
			}
		}

		return super.onKeyUp(keyCode, event);
	}

	private void requestSaveSettings() {
		AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
		LayoutInflater inflater = this.getLayoutInflater();
		final View view = inflater.inflate(R.layout.dialog_controller_setting,
				null);

		// builder.setView(view)
		builder.setMessage(getString(R.string.save_controller_setting))
				.setPositiveButton(R.string.save,
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog, int id) {
								dialog.dismiss();
								for (int i = 0; i < 6; i++) {
									mSelectedId[i] = mSpinner[i]
											.getSelectedItemPosition();

									// one controller only appear once.
									if (mSelectedId[i] != 0) {
										for (int j = 0; j < i; j++) {
											if (mSelectedId[i] == mSelectedId[j]) {
												Toast.makeText(
														mContext,
														getText(R.string.controller_duplication),
														Toast.LENGTH_SHORT)
														.show();
												return;
											}
										}
									}

								}
								mAliasManager.setSelectedId(mSelectedId);
								finish();
							}
						})
				.setNegativeButton(R.string.cancel,
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {
								finish();
							}
						}).create().show();
	}

	@Override
	public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
			long arg3) {
		// 如果未连接则无需计算
		// if (arg2 == 0) {
		// Log.d(TAG, "arg2==0");
		// return;
		// }

		// 计算有几个相同类型的controller
		// int count = 0;
		// for (int i = 0; i < 6; i++) {
		// if (mSpinner[i].getSelectedItemPosition() == arg2) {
		// count++;
		// }
		// }

		// 如果只有一个该类型的controller则不用修改显示名
		// if (count == 1) {
		// return;
		// }

		// 如果有多个该类型的controller则从上往下依次编号
		// for (int i = 0, j = 1; i < 6; i++) {
		// if (mSpinner[i].getSelectedItemPosition() == arg2) {
		// ((TextView) mSpinner[i].getSelectedView())
		// .setText(mArrayAdapter.getItem(arg2) + "" + j);
		// j++;
		// }
		// }

	}

	@Override
	public void onNothingSelected(AdapterView<?> arg0) {
		// TODO Auto-generated method stub

	}

}
